using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace songofbirds
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;

        RenderTarget2D _renderTarget;

        Texture2D _tex_background;
        Texture2D _tex_fence;
        Texture2D _tex_light;
        Texture2D _tex_lightcircle;
        Texture2D _tex_sun;
        Texture2D _tex_bird0;
        Texture2D _tex_bird1;

        Particle2DSystem _circleLightSystem;
        Particle2DSystem _lightSystem;
        Rectangle[] _srcRects;
        Random _r;

        static int LIGHTNUMS = 6;
        static float ADDINGDELAY = 3f;

        float addingcounter = 0;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            TargetElapsedTime = TimeSpan.FromTicks(333333);

            InactiveSleepTime = TimeSpan.FromSeconds(1);

            _graphics.IsFullScreen = true;
        }

        protected override void Initialize()
        {

            base.Initialize();
        }


        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            string dir = "images\\";

            _tex_background = Content.Load<Texture2D>(dir + "background");
            _tex_fence = Content.Load<Texture2D>(dir + "fence");
            _tex_light = Content.Load<Texture2D>(dir + "light");
            _tex_lightcircle = Content.Load<Texture2D>(dir + "lightcircle");
            _tex_sun = Content.Load<Texture2D>(dir + "sun");
            _tex_bird0 = Content.Load<Texture2D>(dir + "bird_0");
            _tex_bird1 = Content.Load<Texture2D>(dir + "bird_1");

            _srcRects = new Rectangle[3];
            _srcRects[0] = new Rectangle(0, 0, 135, 125);
            _srcRects[1] = new Rectangle(135, 0, 105, 125);
            _srcRects[2] = new Rectangle(240, 0, 82, 125);

            _circleLightSystem = new Particle2DSystem(_tex_lightcircle, _srcRects[0], 10, 
                Vector2.One, 2, Vector2.Zero, 0.5f);
            _lightSystem = new Particle2DSystem(_tex_light, null, LIGHTNUMS, Vector2.One, 12, Vector2.Zero, 0.7f);
            _r = new Random();

            _renderTarget = new RenderTarget2D(GraphicsDevice, 800, 480);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            addCirleLight();

            addingcounter += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (addingcounter >= ADDINGDELAY)
            {
                addingcounter -= ADDINGDELAY;
                _lightSystem.AddParticle(new Vector2(750, -50), null, Vector2.Zero, 0, MathHelper.PiOver4*4/3, 0.15f);
            }

            _circleLightSystem.Update();
            _lightSystem.Update();

            base.Update(gameTime);
        }

        private void addLight()
        {
            
        }

        private void addCirleLight()
        {
            // Generate a direction within 15 degrees of (0, 1)
            Vector2 offset = new Vector2(MathHelper.ToRadians(10.0f));
            Vector2 randAngle = randVec2(-offset, offset);
            // Generate a position between (-400, -400) and (400, 400)
            Vector2 randPosition = randVec2(new Vector2(), new Vector2(800, 480));
            // Generate a speed between 600 and 900
            float randSpeed = (float)_r.NextDouble() * 300 + 600;
            int randType = _r.Next(3);
            _circleLightSystem.AddParticle(randPosition, _srcRects[randType], randAngle, randSpeed, 0, 0);
        }

        // Returns a random Vector2 between min and max
        Vector2 randVec2(Vector2 min, Vector2 max)
        {
            return new Vector2(
            min.X + (float)_r.NextDouble() * (max.X - min.X),
            min.Y + (float)_r.NextDouble() * (max.Y - min.Y));
        }

        protected override void Draw(GameTime gameTime)
        {
            
            GraphicsDevice.SetRenderTarget(_renderTarget);
            _spriteBatch.Begin();
            // draw background.
            drawBackGround(gameTime);
            drawBird(gameTime);
            drawForeGround(gameTime);
            _spriteBatch.End();
            GraphicsDevice.SetRenderTarget(null);

            GraphicsDevice.Clear(Color.CornflowerBlue);
            _spriteBatch.Begin();
            for(int x = 0, y; x < 5; x++)
                for(y = 0; y < 5; y++)
                    _spriteBatch.Draw((Texture2D)_renderTarget, new Rectangle(160 * x, 96 * y, 160, 96), Color.White);
            _spriteBatch.End();


            base.Draw(gameTime);
        }

        private void drawForeGround(GameTime gameTime)
        {
            _spriteBatch.Draw(_tex_fence, new Vector2(0, 280), Color.White);
        }

        private void drawBird(GameTime gameTime)
        {
            _spriteBatch.Draw(_tex_bird0, new Vector2(131, 121), Color.White);
        }

        private void drawBackGround(GameTime gameTime)
        {
            _spriteBatch.Draw(_tex_background, Vector2.Zero, Color.White);
            _circleLightSystem.Draw(_spriteBatch);
            _lightSystem.Draw(_spriteBatch);
            _spriteBatch.Draw(_tex_sun, new Vector2(660, -7), Color.White); 
        }

    }
}
